define([
        '../../common/service',
        '../../common/colpick',
        './layoutRight',
        './highEdit',
        '../../../app/pages/createMaterial/step2/chooseDialog',
        'masonry', 'imagesloaded','../../common/modernizr.custom'
    ],
    function(
        http,
        colpick,
        LayoutRightView,
        HighEditView,
        ChooseDialogView, Masonry,Img
    ) {
        var materialCopy = {
            $creativeDrop: {},
            $festivalDrop: {},
            $placeSelect2: {},

            init: function() {
                var _this = this;
                _this.layoutRightView = new LayoutRightView();
                //当前页面移动完毕
                _this.endCurrPage = false;
                //后续页面移动完毕
                _this.endNextPage = false;

                _this.animEndEventNames = {
                    'WebkitAnimation': 'webkitAnimationEnd',
                    'OAnimation': 'oAnimationEnd',
                    'msAnimation': 'MSAnimationEnd',
                    'animation': 'animationend'
                };
                // animation end event name
                _this.animEndEventName = _this.animEndEventNames[Modernizr.prefixed('animation')];
                //保存各个View的默认class
                $(".pt-page").each( function() {
                    var $page = $( this );
                    $page.data( 'originalClassList', $page.attr( 'class' ) );
                } );
                //设置默认页面
                $(".pt-page").eq(0).addClass( 'pt-page-current' );

                _this.getData();
                _this.swapElement();
                _this.openChooseDialog();
                _this.bindSaveBtn();
                var $grid = _this.initMasonry();
                _this.chooseAgain($grid);
                _this.chooseClose();
                _this.bindHighEditAndClose();

            },
            bindHighEditAndClose: function() {
                var _this = this;
                $('.high-edit').off('click').on('click', function(e){
                    var who = $(e.currentTarget).data('who');
                    _this.changeView('#view2');

                    new HighEditView().init(who, function(mainBgColor, mainWordColor, mainEleCode, mainEleId, joinBgColor, joinWordColor, joinEleCode, joinEleId, highImgUrl) {
                        $('input[name=main-element]').attr("bgColor", mainBgColor);
                        $('input[name=main-element]').attr("wordColor", mainWordColor);
                        $('input[name=main-element]').attr("elementCode", mainEleCode);
                        $('input[name=main-element]').attr("templateElementId", mainEleId);
                        $('input[name=lesser-element]').attr("bgColor", joinBgColor);
                        $('input[name=lesser-element]').attr("wordColor", joinWordColor);
                        $('input[name=lesser-element]').attr("elementCode", joinEleCode);
                        $('input[name=lesser-element]').attr("templateElementId", joinEleId);
                        if(highImgUrl != 'ww') {
                            var placementes = _this.$placeSelect2.select2('val');
                            if (placementes && placementes.length > 0) {
                                for (var i = 0; i < placementes.length; i++) {
                                    var placement = placementes[i];
                                    placement = placement.substr(0, placement.indexOf('-'));
                                    placementes[i] = placement;
                                }
                                _this.renderRight(placementes, highImgUrl);
                            }
                        }

                        _this.changeView('#view1')
                    });
                });
                $('.high-close1').off('click').on('click', function(){
                    _this.changeView('#view1')
                });
            },
            /**
             * 所有动画都结束后
             */
            onEndAnimation: function ($outpage, $inpage) {
                var _this = this;
                _this.endCurrPage = false;
                _this.endNextPage = false;
                //resetPage( $outpage, $inpage );
                //isAnimating = false;
                $outpage.attr('class', $outpage.data('originalClassList'));
                $inpage.attr('class', $inpage.data('originalClassList') + ' pt-page-current');
            },
            /**
             * View切换
             * @param newView
             */
            changeView: function (newView) {
                var _this = this;
                var outClass = 'pt-page-flipOutTop';
                var inClass = 'pt-page-flipInBottom pt-page-delay500';

                var $currPage = $(".pt-page-current").eq(0);
                var $nextPage = $(newView);

                //清除原来添加的动画，层级等样式(正常动画结束时会自动清除，这样做防止用户在动画结束前就点击切换其他的)
                $(".pt-page").each(function () {
                    $(this).attr('class', $(this).data('originalClassList'));
                });
                $currPage.addClass("pt-page-current");
                $nextPage.addClass("pt-page-current");


                //如果就是当页则不切换
                if ($currPage.attr("id") == $nextPage.attr("id")) {
                    return;
                }

                //新页面入场
                $currPage.addClass(outClass).on(_this.animEndEventName, function () {
                    $currPage.off(_this.animEndEventName);
                    _this.endCurrPage = true;
                    if (_this.endNextPage) {
                        _this.onEndAnimation($currPage, $nextPage);
                    }
                });

                //旧页面出场
                $nextPage.addClass(inClass).on(_this.animEndEventName, function () {
                    $nextPage.off(_this.animEndEventName);
                    _this.endNextPage = true;
                    if (_this.endCurrPage) {
                        _this.onEndAnimation($currPage, $nextPage);
                    }
                });
            },
            /**
             * 进入页面之后获取相关信息
             */
            getData: function() {
                var _this = this;
                http.work('/ecMaterielCategory/supportCreate', {}, 'POST').then(function(res) {
                    if (res.flag == 'success') {
                        if (res.data == null || res.data.length == 0) {
                            return;
                        }
                        _this.supportCreateData = res.data; //将数据缓存起来
                        //初始化 下拉菜单
                        $.each(res.data, function(i, obj) {
                            if (obj.categoryType == "adPlaceMent") {
                                var _data = [];
                                $.each(obj.children, function(j, oo) {
                                    var temp = {
                                        id: oo.categoryType + "-" + oo.id + "-" + oo.pid,
                                        text: oo.categoryName
                                    }
                                    _data.push(temp);
                                });
                                _this.$placeSelect2 = _this.initPlaceSelect2(_data);
                            } else if (obj.categoryType == "adTarget") {
                                var _data = [],
                                    _data2 = [];
                                $.each(obj.children, function(j, oo) {
                                    var temp = {
                                        id: oo.id,
                                        text: oo.categoryName,
                                        val: oo.id
                                    }
                                    _data.push(temp);
                                    if ("festival" == oo.categoryType) {
                                        $.each(oo.children, function(k, ooo) {
                                            var temp2 = {
                                                id: ooo.id,
                                                text: ooo.categoryName,
                                                val: ooo.id
                                            }
                                            _data2.push(temp2);
                                        });
                                    }
                                });
                                _this.$creativeDrop = _this.initCreativesDrop(_data);
                                _this.$festivalDrop = _this.initFestivalDrop(_data2);
                            }
                        });
                        _this._setData();
                    }
                });

            },
            _setData: function() {
                var _this = this;
                var id = Kit.getUrlParam('id');
                if (Kit.isBlank(id)) {
                    console.log('参数错误！' + id);
                }
                http.work('/ecTemplateMateriel/findByEditOrCopy', { id: id }, 'POST').then(function(res) {
                    if (res.flag == 'success') {
                        if (res.data == null || res.data.length == 0) {
                            return;
                        }
                        //填充值
                        //set layout-left
                        _this._setLayoutLeft(res.data);
                        //set layout-center
                        _this._setLayoutCenter(res.data);

                        _this._setLayoutRight(res.data);

                    }
                });
            },
            setSaveData: function($dom, key, id, pid, categoryType, title) {
                var temp = {
                    id: id,
                    pid: pid,
                    checked: true,
                    categoryType: categoryType
                }
                if (title != undefined) {
                    temp.title = title;
                }
                temp = _.isArray(temp) ? temp : [temp];
                Kit.setCache('copyModule', key, temp);
            },
            _setLayoutLeft: function(datas) {
                var _this = this;
                $('input[name=feed-name]').val(datas.productWeFeed.feedName);
                $('input[name=feed-name]').attr("data-id", datas.productWeFeed.id);
                $('input[name=feed-name]').attr("data-templateId", datas.ecTemplateMateriel.templateId);
                $('input[name=feed-name]').attr("data-ecTemplateMaterielId", datas.ecTemplateMateriel.id);
                $('span[name=feed-name]').html(datas.productWeFeed.feedName);
                $.each(datas.ecMaterielCategoryList, function(i, obj) {
                    if (obj.categoryType == "creativeType") {
                        $('input[name=feed-style]').val(obj.children[0].categoryName);
                        $('span[name=feed-style]').html(obj.children[0].categoryName);
                        _this.setSaveData($('input[name=feed-style]'), 'creativeType', obj.children[0].id, obj.children[0].pid, obj.children[0].categoryType);
                    } else if (obj.categoryType == "adPlaceMent") {
                        var temp = [],
                            saveTemp = [];
                        $.each(obj.children, function(i, o) {
                            var _v = o.categoryType + "-" + o.id + "-" + o.pid;
                            temp.push(_v);
                            var saveTempObj = {
                                id: o.id,
                                pid: o.pid,
                                checked: true,
                                categoryType: o.categoryType,
                                title: o.categoryName
                            }
                            saveTemp.push(saveTempObj);
                        });
                        $("select[name=place-select2]").select2('val', temp);
                        Kit.setCache('copyModule', 'adPlaceMent', saveTemp);
                    } else if (obj.categoryType == "adTarget") {
                        _this.$creativeDrop.val(obj.children[0].categoryName);
                        //显示 节日
                        $("span[name=inline-drop]").html(obj.children[0].categoryName);
                        _this.setSaveData(_this.$creativeDrop, 'adTarget', obj.children[0].id, obj.children[0].pid, obj.children[0].categoryType, obj.children[0].categoryName);
                        if (obj.children[0].categoryType == "festival" && obj.children[0].children[0]) {
                            //是节日的话，显示节日的下拉框并赋值
                            var children = obj.children[0].children[0];
                            _this.$festivalDrop.val(obj.children[0].children[0].categoryName);
                            $("span[name=inline-drop]").html(obj.children[0].categoryName +"-"+ obj.children[0].children[0].categoryName);
                            //_this.$festivalDrop.show();
                            _this.setSaveData(_this.$festivalDrop, 'festival', children.id, children.pid, children.categoryType, children.categoryName);
                        }
                    }else if(obj.categoryType == "adCarousel"){
                        var saveTemp = [];
                        $.each(obj.children, function(i, o) {
                            var saveTempObj = {
                                id: o.id,
                                pid: o.pid,
                                checked: true,
                                categoryType: o.categoryType,
                                title: o.categoryName
                            }
                            saveTemp.push(saveTempObj);
                        });
                        console.log(saveTemp);
                        Kit.setCache('copyModule', 'adCarousel', saveTemp);
                    }
                });
            },
            _setLayoutCenter: function(datas) {
                //投放中的物料，在修改时只能编辑展示位置。 ---esa     所以这部分禁止掉
                var status = Kit.getUrlParam('status');
                if (status == "running") {
                    $('.layout-center-hidden').show();
                }

                $.each(datas.ecMaterielElements, function(i, obj) {
                    if (obj.major) {
                        $('input[name=main-element]').val(obj.context);
                        $('input[name=main-element]').attr("pid", obj.pid);
                        $('input[name=main-element]').attr("templateElementId", obj.id);
                        $('input[name=main-element]').attr("major", obj.major);
                        $('input[name=main-element]').attr("code", obj.parentCode);
                        $('input[name=main-element]').attr("elementCode", obj.elementCode);
                        //如果有设置n,则显示出n
                        if (obj.property == 2) {
                            $('input[name=main-element]').nextAll('.element-num').css('visibility', 'visible');
                            $('input[name=main-element]').nextAll('.element-num').find('input').val(obj.fillValue);
                        }
                        Kit.setCache("createMaterialStep2", 'parentCategory1', obj.pid);
                    } else {
                        $('input[name=lesser-element]').val(obj.context);
                        $('input[name=lesser-element]').attr("pid", obj.pid);
                        $('input[name=lesser-element]').attr("templateElementId", obj.id);
                        $('input[name=lesser-element]').attr("major", obj.major);
                        $('input[name=lesser-element]').attr("code", obj.parentCode);
                        $('input[name=lesser-element]').attr("elementCode", obj.elementCode);
                        //如果有设置n,则显示出n
                        if (obj.property == 2) {
                            $('input[name=lesser-element]').nextAll('.element-num').css('visibility', 'visible');
                            $('input[name=lesser-element]').nextAll('.element-num').find('input').val(obj.fillValue);
                        }
                        Kit.setCache("createMaterialStep2", 'parentCategory-1', obj.pid);
                    }
                });
            },
            _setLayoutRight: function(data) {
                $('.layout-right').data('name', data.ecTemplateMateriel.name);
                $('.layout-right').data('id', data.ecTemplateMateriel.id);
                var adPlaceMent = _.findWhere(data.ecMaterielCategoryList, { categoryType: 'adPlaceMent' });
                var adPlaces = [];
                _.each(adPlaceMent.children, function(el) {
                    adPlaces.push(el.categoryType);
                });
                var previewImg = data.ecTemplateMateriel.thumbnail;
                window.localStorage.setItem("previewImg", previewImg);
                window.localStorage.setItem("previewImg-width", data.ecTemplateMateriel.width);
                this.renderRight(adPlaces, previewImg);
            },
            initPlaceSelect2: function(datas) {
                var _this = this;
                var $placeSelect2 = $("select[name=place-select2]").select2({
                    data: datas,
                    placeholder: '请选择展示位置',
                    multiple: true,
                    allowClear: false,
                    width: '100%'
                }).off("select2:select").on("select2:select", function(e) {
                    //self.showGenerate();
                    var valueArr = $(this).select2("val");
                    var $placeTitles = $(".select2-container li.select2-selection__choice");
                    var saveTemp = [];
                    $.each(valueArr, function(i, o) { // valueArr = ["mobilefeed-14-3", "mobileexternal-16-3"]
                        var tempObj = o.split("-");
                        var saveTempObj = {
                            id: tempObj[1],
                            pid: tempObj[2],
                            checked: true,
                            categoryType: tempObj[0],
                            title: $placeTitles.eq(i).attr('title')
                        }
                        saveTemp.push(saveTempObj);
                    });
                    //渲染右边预览
                    if (valueArr && valueArr.length > 0) {
                        for (var i = 0; i < valueArr.length; i++) {
                            var placement = valueArr[i];
                            placement = placement.substr(0, placement.indexOf('-'));
                            valueArr[i] = placement;
                        }
                        var imgObj = window.localStorage.getItem("previewImg");
                        _this.renderRight(valueArr, imgObj);
                    }
                    Kit.setCache('copyModule', 'adPlaceMent', saveTemp);
                }).off("select2:unselect").on("select2:unselect", function(e) {
                    //self.showGenerate();
                    var valueArr = $(this).select2("val") == null?[]:$(this).select2("val");
                    var $placeTitles = $(".select2-container li.select2-selection__choice");
                    var saveTemp = [];
                    $.each(valueArr, function(i, o) { // valueArr = ["mobilefeed-14-3", "mobileexternal-16-3"]
                        var tempObj = o.split("-");
                        var saveTempObj = {
                            id: tempObj[1],
                            pid: tempObj[2],
                            checked: true,
                            categoryType: tempObj[0],
                            title: $placeTitles.eq(i).attr('title')
                        }
                        saveTemp.push(saveTempObj);
                    });
                    //渲染右边预览
                    if (valueArr && valueArr.length > 0) {
                        for (var i = 0; i < valueArr.length; i++) {
                            var placement = valueArr[i];
                            placement = placement.substr(0, placement.indexOf('-'));
                            valueArr[i] = placement;
                        }
                        var imgObj = window.localStorage.getItem("previewImg");
                        _this.renderRight(valueArr, imgObj);
                    }
                    Kit.setCache('copyModule', 'adPlaceMent', saveTemp);
                });
                return $placeSelect2;
            },
            initCreativesDrop: function(datas) {
                var _this = this;
                var $creativeDrop = $("div.ndp-drop-wrapper[name=inline-drop-1]").drop({
                    data: datas,
                    name: "creatives" //为了方便serialize 建议设置name 属性
                }).off("ITEM_CLICK").on("ITEM_CLICK", function(e) { //下拉选择项点击事件
                    //返回的数据有 _this.supportCreateData
                    var adTargets = _.findWhere(_this.supportCreateData, { categoryType: 'adTarget' });
                    var curAdTarget = _.findWhere(adTargets.children, { id: e.originalEvent.data.val * 1 });
                    _this.setSaveData(_this.$creativeDrop, 'adTarget', curAdTarget.id, curAdTarget.pid, curAdTarget.categoryType, curAdTarget.categoryName);
                    var festivals = _.findWhere(adTargets.children, { categoryType: 'festival' });
                    var firstFestival = {};
                    if (curAdTarget.categoryType && curAdTarget.categoryType == 'festival') {
                        if (festivals && festivals.children && festivals.children[0]) {
                            var firstFestival = festivals.children[0];
                        }
                        _this.setSaveData(_this.$festivalDrop, 'festival', firstFestival.id, firstFestival.pid, firstFestival.categoryType, firstFestival.categoryName);
                        $('div[name=inline-drop-2]').find('input').val(firstFestival.categoryName);
                        //$('div[name=inline-drop-2]').show();
                    } else {
                        $('div[name=inline-drop-2]').hide();
                        Kit.setCache('copyModule', 'festival', []);
                    }
                });
                return $creativeDrop;
            },
            initFestivalDrop: function(datas) {
                var _this = this;
                var $festivalDrop = $("div.ndp-drop-wrapper[name=inline-drop-2]").drop({
                    data: datas,
                    name: "festival" //为了方便serialize 建议设置name 属性
                }).off("ITEM_CLICK").on("ITEM_CLICK", function(e) { //下拉选择项点击事件
                    //返回的数据有
                    var adTargets = _.findWhere(_this.supportCreateData, { categoryType: 'adTarget' });
                    var festivals = _.findWhere(adTargets.children, { categoryType: 'festival' });
                    var curFestival = _.findWhere(festivals.children, { id: e.originalEvent.data.val * 1 });
                    _this.setSaveData(_this.$festivalDrop, 'festival', curFestival.id, curFestival.pid, curFestival.categoryType, curFestival.categoryName);
                });
                return $festivalDrop;
            },
            renderRight: function(adPlaceMent, previewImg) {
                this.layoutRightView.init($('.fmp-container .layout-right'), adPlaceMent, previewImg);
            },
            swapElement: function() {
                $(".step2-swich-btn-div").off('click').on('click', function(e) {
                    var swap = $(e.currentTarget).data('swap') * -1;
                    var $major = $('#majorElement');
                    var $minor = $('#minorElement');
                    var $majorFill = $('#majorFill');
                    var $minorFill = $('#minorFill');

                    $major.attr('type-a', swap);
                    $minor.attr('type-a', swap * -1);

                    var top = swap > 0 ? 0 : 145 * swap;
                    $major.parent().animate({ top: top * -1 })
                    $minor.parent().animate({ top: top })
                    $(e.currentTarget).data('swap', swap);
                });
            },
            openChooseDialog: function() {
                $('.btn-select').click(function(e) {
                    var who = $(e.currentTarget).data('who')
                    new ChooseDialogView().init(who, function(pid, code, prop, context) {
                        var input = $(e.currentTarget).prev('input');
                        input.val(context);
                        input.attr('pid', pid);
                        input.attr('code', code);
                        input.attr('prop', prop);
                        input.attr('elementCode','');
                        input.attr('templateElementId','');
                        if (prop == 2) {
                            $(e.currentTarget).next('.element-num').css('visibility', 'visible');
                        } else {
                            $(e.currentTarget).next('.element-num').css('visibility', 'hidden');
                        }
                    });
                });
            },
            bindSaveBtn: function() {
                var self = this;
                $('button[name=saveCreative]').click(function() {
                    kit.loading.show($('.fmp-article'));
                    self.bindCreative(self.getParam());
                });
                //点击取消 按钮
                $('button.btn-cancel').click(function() {
                    window.location.hash = "#!materialTemplate";
                });
            },
            getParam: function() {
                var params = {
                    "wefeedid": $('input[name=feed-name]').attr("data-id"),
                    "more": false, //查看更多 or not
                    "ecTemplateMaterielId":$('input[name=feed-name]').attr("data-ecTemplateMaterielId"),
                    "ecTemplateId": $('input[name=feed-name]').attr("data-templateId"),
                    "elements": [{
                        parentCode: $('input[name=main-element]').attr('code'),
                        elementCode:$('input[name=main-element]').attr('elementCode'),
                        "templateElementId": $('input[name=main-element]').attr("templateElementId"),
                        "major": ($('#majorElement').attr('type-a') > 0), //1主要元素 0 次要元素
                        "fillType": $('input[name=main-element]').attr("prop"), //0不用输入值 1半输入 2全输入
                        "fillValue": $('input[name=main-element]').nextAll('.element-num').find('input').val()
                    }, {
                        parentCode: $('input[name=lesser-element]').attr('code'),
                        elementCode:$('input[name=lesser-element]').attr('elementCode'),
                        "templateElementId": $('input[name=lesser-element]').attr("templateElementId"),
                        "major": ($('#minorElement').attr('type-a') > 0), //1主要元素 0 次要元素
                        "fillType": $('input[name=lesser-element]').attr("prop"), //0不用输入值 1半输入 2全输入
                        "fillValue": $('input[name=lesser-element]').nextAll('.element-num').find('input').val()
                    }]
                };
                return params;
            },
            bindCreative: function(param) {
                var _this = this;
                http.work('/ecTemplateMateriel/buildCreative', param, 'POST').then(function(res) {
                    if (res.flag == 'success') {
                        if (res.data == null || res.data.length == 0) {
                            return;
                        }
                        _this.goOnBindCreative(res.data[0]);
                    }
                });
            },
            goOnBindCreative: function(jobId) {
                    var _this = this;
                    http.work('/ecTemplateMateriel/buildCreativeResult', { jobId: jobId }, 'POST').then(function(res) {
                        if (res.flag == 'success') {
                            if (res.data == null || res.data.length == 0) {
                                kit.loading.hide($('.fmp-article'));
                                return;
                            }
                            if (res.data.status == 1) {
                                console.log("继续请求后台，已创建为：" + res.data.createdFeedSum + "总数为：" + res.data.originalFeedSum);
                                setTimeout(function() {
                                    _this.goOnBindCreative.call(_this, jobId);
                                }, 2000);
                            } else {
                                //完成
                                // set layout 图片
                                var previewImg = res.data.feedList[0].image_link;
                                var placementes = _this.$placeSelect2.select2('val');
                                if (placementes && placementes.length > 0) {
                                    for (var i = 0; i < placementes.length; i++) {
                                        var placement = placementes[i];
                                        placement = placement.substr(0, placement.indexOf('-'));
                                        placementes[i] = placement;
                                    }
                                    _this.renderRight(placementes, previewImg);
                                    kit.loading.hide($('.fmp-article'));
                                }
                            }
                        }else{
                            kit.loading.hide($('.fmp-article'));
                        }
                    });
                }
                /**
                 * 点击重新选择
                 */
                ,
            chooseAgain: function($grid) {
                var _this = this;
                $('.chooseAgain.preview-span>a').click(function() {
                    //显示列表
                    $(".layout").addClass("hidden");
                    $(".layout-bottom").removeClass("hidden");
                    var param = _this.getParam();
                    param.ecTemplateId = 0;  //之前需求要求这里传个0，现在要加一个options
                    param.options = {
                        creativeType:Kit.getCache("copyModule","creativeType"),
                        adPlaceMent:Kit.getCache("copyModule","adPlaceMent"),
                        adTarget:Kit.getCache("copyModule","adTarget"),
                        adCarousel:Kit.getCache("copyModule","adCarousel")
                    }
                    if(Kit.getCache("copyModule","festival") != undefined){
                        param.options.festival = Kit.getCache("copyModule","festival");
                    }
                    _this.bindCreative2(param, $grid);
                });
            },
            chooseClose: function() {
                $('.choose-head').find('span:last').click(function() {
                    $(".layout").removeClass("hidden");
                    $(".layout-bottom").addClass("hidden");
                });
            },
            initMasonry: function() {
                $.bridget('masonry', Masonry, $);
                var $grid = $('.grid').masonry({
                    itemSelector: '.grid-item',
                    percentPosition: true,
                    columnWidth: '.grid-sizer'
                });
                $('.grid').imagesLoaded().progress(function() {
                    $grid.masonry('layout');
                });
                return $grid;
            },
            bindCreative2: function(param, $grid) {
                var _this = this;
                $('#masonryDom').empty();
                $('.btn-choose-save').addClass('disabled');
                $('.choose-hide').removeClass("hidden");
                http.work('/ecTemplateMateriel/buildCreative', param, 'POST').then(function(res) {
                    if (res.flag == 'success') {
                        if (res.data == null || res.data.length == 0) {
                            return;
                        }
                        $.each(res.data, function(i, obj) {
                            _this.goOnBindCreative2($grid, obj);
                        });
                    }
                });
            },
            goOnBindCreative2: function($grid, jobId) {
                var _this = this;
                http.work('/ecTemplateMateriel/buildCreativeResult', { jobId: jobId }, 'POST').then(function(res) {
                    if (res.flag == 'success') {
                        if (res.data == null || res.data.length == 0) {
                            return;
                        }
                        if (res.data.status == 1) {
                            console.log("继续请求后台，已创建为：" + res.data.createdFeedSum + "总数为：" + res.data.originalFeedSum);
                            setTimeout(function() {
                                _this.goOnBindCreative2.call(_this, $grid, jobId);
                            }, 2000);
                        } else {
                            //完成
                            $('.btn-choose-save').removeClass('disabled');
                            $('.choose-hide').addClass("hidden");
                            var previewImg = res.data.feedList[0].image_link;
                            var _html = '<div data-id="' + res.data.ecTemplate.id + '" class="grid-item"><img src="' + previewImg + '"/> </div>';
                            _this.buildCreativeResult2 = res.data; //将数据缓存起来
                            $grid.append(_html).masonry('appended', $(_html));
                            _this.chooseSaveEvent();
                        }
                    }
                });
            },
            chooseSaveEvent: function() {
                var _this = this;
                $('#masonryDom .grid-item').click(function() {
                    $('#masonryDom .grid-item').removeClass('choose');
                    $(this).addClass("choose");
                });
                $('button.btn-choose-save').click(function() {
                    //set image
                    if ($(this).hasClass('disabled')) {
                        return;
                    }
                    var previewImg = $('.grid-item.choose img').attr("src");
                    if ($('.grid-item.choose img').attr("src") == undefined) {
                        Kit.alert2(I18N.getText('materialCopy', 'chooseTemplateMaterial'));
                    } else {
                        $('input[name=feed-name]').attr("data-templateId", _this.buildCreativeResult2.ecTemplate.id);
                        $(".layout").removeClass("hidden");
                        $(".layout-bottom").addClass("hidden");
                        var placementes = _this.$placeSelect2.select2('val');
                        if (placementes && placementes.length > 0) {
                            for (var i = 0; i < placementes.length; i++) {
                                var placement = placementes[i];
                                placement = placement.substr(0, placement.indexOf('-'));
                                placementes[i] = placement;
                            }
                            _this.renderRight(placementes, previewImg);
                        }
                    }
                });
            }
        }
        return materialCopy;
    });
